﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace AutoSPInstallerGUI.Classes
{
    /// <summary>
    /// Used to convert string values to boolean
    /// </summary>
    public class StringtoBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string stringValue = value as string;
            if (stringValue != null)
            {
                return String.Equals(stringValue, parameter.ToString(), StringComparison.CurrentCultureIgnoreCase);
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return parameter;
        }
    }

    /// <summary>
    /// Used to convert string values to boolean inverse
    /// </summary>
    public class InverseStringtoBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string stringValue = value as string;
            if (stringValue != null)
            {
                return !String.Equals(stringValue, parameter.ToString(), StringComparison.CurrentCultureIgnoreCase);
            }
            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return parameter;
        }
    }

    /// <summary>
    /// Used to for inverting Boolean bindings
    /// </summary>
    [ValueConversion(typeof(bool), typeof(bool))]
    public class InverseBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(bool))
            {
                throw new InvalidOperationException("The target must be a Boolean");
            }

            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    /// <summary>
    /// Returns true if parameter contains value
    /// </summary>
    public class CollectionOfStringtoBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string stringValue = value as string;
            if (stringValue != null)
            {
                string s = parameter as string;
                if (s != null)
                {
                    string[] stringParameters = s.Split(";".ToCharArray());

                    foreach (string item in stringParameters)
                    {
                        if (item.ToLower() == stringValue.ToLower())
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    /// <summary>
    /// Returns false if parameter contains value
    /// </summary>
    public class InverseCollectionOfStringtoBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string stringValue = value as string;
            if (stringValue != null)
            {
                string s = parameter as string;
                if (s != null)
                {
                    string[] stringParameters = s.Split(";".ToCharArray());

                    foreach (string item in stringParameters)
                    {
                        if (item.ToLower() == stringValue.ToLower())
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}